<?php
namespace Platform\Api;
use PhalApi\Api;
use Platform\Domain\Articles as ArticlesDomain;
use Platform\Domain\LeaveMessage as LeaveMessageDomain;
use Platform\Domain\ContactInformation as ContactInformationDomain;

/**
 * 首页接口类
 * @author: dexter
 */
class Home extends Api {
    public function getRules() {
        return array(
            'messageList' => array(
                'page' => array('name' => 'page', 'require' => false, 'desc' => '当前页，默认1'),
                'limit' => array('name' => 'limit', 'require' => false, 'desc' => '一页展示数量默认20'),
            ),
            'addMessage' => array(
                'name' => array('name' => 'name', 'require' => false, 'desc' => '姓名'),
                'contact' => array('name' => 'contact', 'require' => false, 'desc' => '联系方式'),
                'content' => array('name' => 'content', 'require' => false, 'desc' => '留言信息'),
                'type' => array('name' => 'type', 'require' => false, 'desc' => '留言类型 1学生家长 2学生'),
            ),
            'detailMessage' => array(
                'id' => array('name' => 'id', 'require' => true, 'desc' => '当前选中的id'),
            ),
            'editMessage' => array(
                'name' => array('name' => 'name', 'require' => false, 'desc' => '姓名'),
                'contact' => array('name' => 'contact', 'require' => false, 'desc' => '联系方式'),
                'content' => array('name' => 'content', 'require' => false, 'desc' => '留言信息'),
                'type' => array('name' => 'type', 'require' => false, 'desc' => '留言类型 1学生家长 2学生'),
            ),
            'deleteMessage' => array(
                'id' => array('name' => 'id', 'require' => true, 'desc' => '当前选中的id'),
            ),
            'articlesList' => array(
                'page' => array('name' => 'page', 'require' => false, 'desc' => '当前页，默认1'),
                'limit' => array('name' => 'limit', 'require' => false, 'desc' => '一页展示数量默认20'),
                'module_type' => array('name' => 'module_type', 'require' => false, 'desc' => '所属模块'),
                'title' => array('name' => 'title', 'require' => false, 'desc' => '标题'),
                'order' => array('name' => 'order', 'require' => false, 'desc' => '排序默认按sort，hot热门阅读量最多排序，new最新更新排序'),
            ),
            'addArticles' => array(
                'title' => array('name' => 'title', 'require' => false, 'desc' => '标题'),
                'subtitle' => array('name' => 'subtitle', 'require' => false, 'desc' => '副标题'),
                'img_url' => array('name' => 'img_url', 'require' => false, 'desc' => '封面地址'),
                'content' => array('name' => 'content', 'require' => false, 'desc' => '内容'),
                'sort' => array('name' => 'sort', 'require' => false, 'desc' => '排序0开始，越大越靠前'),
                'outer_chain' => array('name' => 'outer_chain', 'require' => false, 'desc' => '外链地址'),'add_time' => array('name' => 'add_time', 'require' => false, 'desc' => '添加时间'),
                'module_type' => array('name' => 'module_type', 'require' => false, 'desc' => '所属模块'),
            ),
            'detailArticles' => array(
                'id' => array('name' => 'id', 'require' => true, 'desc' => '当前选中的id'),
            ),
            'editArticles' => array(
                'id' => array('name' => 'id', 'require' => true, 'desc' => '当前选中的id'),
                'title' => array('name' => 'title', 'require' => false, 'desc' => '标题'),
                'subtitle' => array('name' => 'subtitle', 'require' => false, 'desc' => '副标题'),
                'img_url' => array('name' => 'img_url', 'require' => false, 'desc' => '封面地址'),
                'content' => array('name' => 'content', 'require' => false, 'desc' => '内容'),
                'sort' => array('name' => 'sort', 'require' => false, 'desc' => '排序0开始，越大越靠前'),
                'outer_chain' => array('name' => 'outer_chain', 'require' => false, 'desc' => '外链地址'),'add_time' => array('name' => 'add_time', 'require' => false, 'desc' => '添加时间'),
                'module_type' => array('name' => 'module_type', 'require' => false, 'desc' => '所属模块'),
            ),
            'deleteArticles' => array(
                'id' => array('name' => 'id', 'require' => true, 'desc' => '当前选中的id'),
            ),

            'contactInformationList' => array(
                'page' => array('name' => 'page', 'require' => false, 'desc' => '当前页，默认1'),
                'limit' => array('name' => 'limit', 'require' => false, 'desc' => '一页展示数量默认20'),
            ),
            'addContactInformation' => array(
                'email' => array('name' => 'email', 'require' => false, 'desc' => '邮箱'),
                'address' => array('name' => 'address', 'require' => false, 'desc' => '地址'),
                'phone' => array('name' => 'phone', 'require' => false, 'desc' => '电话'),
                'tax' => array('name' => 'tax', 'require' => false, 'desc' => '传真'),
            ),
            'detailContactInformation' => array(
                'id' => array('name' => 'id', 'require' => true, 'desc' => '当前选中的id'),
            ),
            'editContactInformation' => array(
                'email' => array('name' => 'email', 'require' => false, 'desc' => '邮箱'),
                'address' => array('name' => 'address', 'require' => false, 'desc' => '地址'),
                'phone' => array('name' => 'phone', 'require' => false, 'desc' => '电话'),
                'tax' => array('name' => 'tax', 'require' => false, 'desc' => '传真'),
            ),
            'deleteContactInformation' => array(
                'id' => array('name' => 'id', 'require' => true, 'desc' => '当前选中的id'),
            ),
        );
    }

    /**
     * 留言列表接口
     * @desc banner列表接口
     * @return int id 主键id
     * @return string name 姓名
     * @return string contact 联系方式
     * @return string content 留言信息
     * @return string add_time 添加时间
     * @return int type 留言类型 1学生家长 2学生
     */
    public function messageList() {
        $domain = new LeaveMessageDomain();
        $page = $this->page?$this->page:1;
        $limit = $this->limit?$this->limit:20;
        $where = array();
        return $domain->getList($where,$page,$limit);
    }

    /**
     * 新增留言
     * @desc 新增留言
     */
    public function addMessage() {
        $domain = new LeaveMessageDomain();
        if( isset($this->name) && !empty($this->name) ){
            $data['name'] = $this->name;
        }
        if( isset($this->contact) && !empty($this->contact) ){
            $data['contact'] = $this->contact;
        }
        if( isset($this->content) && !empty($this->content) ){
            $data['content'] = $this->content;
        }
        if( isset($this->type) && !empty($this->type) ){
            $data['type'] = $this->type;
        }
        return $domain->add($data);
    }

    /**
     * 留言详情接口
     * @desc 留言详情接口
     * @return int id 主键id
     * @return string name 姓名
     * @return string contact 联系方式
     * @return string content 留言信息
     * @return string add_time 添加时间
     * @return int type 留言类型 1学生家长 2学生
     */
    public function detailMessage() {
        $domain = new LeaveMessageDomain();
        return $domain->detail($this->id);
    }

    /**
     * 编辑提交留言
     * @desc 编辑提交留言
     */
    public function editMessage() {
        $domain = new LeaveMessageDomain();
        if( isset($this->name) && !empty($this->name) ){
            $data['name'] = $this->name;
        }
        if( isset($this->contact) && !empty($this->contact) ){
            $data['contact'] = $this->contact;
        }
        if( isset($this->content) && !empty($this->content) ){
            $data['content'] = $this->content;
        }
        if( isset($this->type) && !empty($this->type) ){
            $data['type'] = $this->type;
        }
        return $domain->edit($this->id,$data);
    }

    /**
     *  删除留言
     * @desc 删除留言
     */
    public function deleteMessage() {
        $domain = new LeaveMessageDomain();
        return $domain->delete($this->id);
    }

    /**
     * 文章相关列表接口
     * @desc 文章相关列表接口
     * @return int id 主键id
     * @return string title 标题
     * @return string subtitle 副标题
     * @return string img_url 封面地址
     * @return string content 内容
     * @return string sort 排序0开始，越大越靠前
     * @return string outer_chain 外链地址
     * @return string add_time 添加时间
     * @return string update_time 更新时间
     * @return string module_type 所属模块
     */
    public function articlesList() {
        $domain = new ArticlesDomain();
        $page = $this->page?$this->page:1;
        $limit = $this->limit?$this->limit:20;
        $where = array();
        $order = 'sort DESC';
        if( isset($this->module_type) && !empty( $this->module_type ) ){
            $where['module_type'] = $this->module_type;
        }
        if( isset($this->title) && !empty( $this->title ) ){
            $where['title like ?'] = '%'.$this->title.'%';
        }
        if( isset($this->order) && !empty( $this->order ) ){
            if( $this->order == 'new' ){
                $order = 'update_time DESC';
            }elseif( $this->order == 'hot' ){
                $order = 'read_num DESC';
            }
        }
        $order .= ',id DESC';
        return $domain->getList($where,$page,$limit,$order);
    }

    /**
     * 新增文章相关
     * @desc 新增文章相关
     */
    public function addArticles() {
        $domain = new ArticlesDomain();
        if( isset($this->title) && !empty($this->title) ){
            $data['title'] = $this->title;
        }
        if( isset($this->subtitle) && !empty($this->subtitle) ){
            $data['subtitle'] = $this->subtitle;
        }
        if( isset($this->img_url) && !empty($this->img_url) ){
            $data['img_url'] = $this->img_url;
        }
        if( isset($this->content)){
            $data['content'] = $this->content;
        }
        if( isset($this->sort) ){
            $data['sort'] = $this->sort;
        }
        if( isset($this->outer_chain) && !empty($this->outer_chain) ){
            $data['outer_chain'] = $this->outer_chain;
        }
        if( isset($this->add_time) && !empty($this->add_time) ){
            $data['add_time'] = $this->add_time;
        }
        if( isset($this->module_type) && !empty($this->module_type) ){
            $data['module_type'] = $this->module_type;
        }
        return $domain->add($data);
    }

    /**
     * 文章相关详情接口
     * @desc 文章相关详情接口
     * @return int id 主键id
     * @return string title 标题
     * @return string subtitle 副标题
     * @return string img_url 封面地址
     * @return string content 内容
     * @return string sort 排序0开始，越大越靠前
     * @return string outer_chain 外链地址
     * @return string add_time 添加时间
     * @return string update_time 更新时间
     */
    public function detailArticles() {
        $domain = new ArticlesDomain();
        return $domain->detail($this->id);
    }

    /**
     * 编辑提交文章相关
     * @desc 编辑提交文章相关
     */
    public function editArticles() {
        $domain = new ArticlesDomain();
        if( isset($this->title) && !empty($this->title) ){
            $data['title'] = $this->title;
        }
        if( isset($this->subtitle) && !empty($this->subtitle) ){
            $data['subtitle'] = $this->subtitle;
        }
        if( isset($this->img_url) && !empty($this->img_url) ){
            $data['img_url'] = $this->img_url;
        }
        if( isset($this->content)){
            $data['content'] = $this->content;
        }
        if( isset($this->sort) ){
            $data['sort'] = $this->sort;
        }
        if( isset($this->outer_chain) && !empty($this->outer_chain) ){
            $data['outer_chain'] = $this->outer_chain;
        }
        if( isset($this->add_time) && !empty($this->add_time) ){
            $data['add_time'] = $this->add_time;
        }
        if( isset($this->module_type) && !empty($this->module_type) ){
            $data['module_type'] = $this->module_type;
        }
        return $domain->edit($this->id,$data);
    }

    /**
     *  删除文章相关
     * @desc 删除文章相关
     */
    public function deleteArticles() {
        $domain = new ArticlesDomain();
        return $domain->delete($this->id);
    }

    /**
     * 联系方式列表接口
     * @desc 联系方式列表接口
     * @return int id 主键id
     * @return string email 邮箱
     * @return string address 地址
     * @return string phone 电话
     * @return string tax 传真
     */
    public function contactInformationList() {
        $domain = new ContactInformationDomain();
        $page = $this->page?$this->page:1;
        $limit = $this->limit?$this->limit:20;
        $where = array();
        return $domain->getList($where,$page,$limit);
    }

    /**
     * 添加联系方式
     * @desc 添加联系方式
     */
    public function addContactInformation() {
        $domain = new ContactInformationDomain();
        if( isset($this->email) && !empty($this->email) ){
            $data['email'] = $this->email;
        }
        if( isset($this->address) && !empty($this->address) ){
            $data['address'] = $this->address;
        }
        if( isset($this->phone) && !empty($this->phone) ){
            $data['phone'] = $this->phone;
        }
        if( isset($this->tax) && !empty($this->tax) ){
            $data['tax'] = $this->tax;
        }
        return $domain->add($data);
    }

    /**
     * 联系方式详情接口
     * @desc 联系方式详情接口
     * @return int id 主键id
     * @return string email 邮箱
     * @return string address 地址
     * @return string phone 电话
     * @return string tax 传真
     */
    public function detailContactInformation() {
        $domain = new ContactInformationDomain();
        return $domain->detail($this->id);
    }

    /**
     * 编辑提交联系方式
     * @desc 编辑提交联系方式
     */
    public function editContactInformation() {
        $domain = new ContactInformationDomain();
        if( isset($this->email) && !empty($this->email) ){
            $data['email'] = $this->email;
        }
        if( isset($this->address) && !empty($this->address) ){
            $data['address'] = $this->address;
        }
        if( isset($this->phone) && !empty($this->phone) ){
            $data['phone'] = $this->phone;
        }
        if( isset($this->tax) && !empty($this->tax) ){
            $data['tax'] = $this->tax;
        }
        return $domain->edit($this->id,$data);
    }

    /**
     *  删除联系方式
     * @desc 删除联系方式
     */
    public function deleteContactInformation() {
        $domain = new ContactInformationDomain();
        return $domain->delete($this->id);
    }
}
